package com.sgb.kjwl.view.ui.activity.self.authentication;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityTeamAuthBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.TeamPerson;
import com.sgb.kjwl.view.ui.adapter.self.mine.TeamAuthAdapter;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.SuccessEntity;
import com.sgb.kjwl.model.entity.self.TeamAuthEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.service.CodeTimerService;
import com.sgb.link.view.widget.OneAndTwoLevelMultipleChoiceWindow;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.event.self.AuthenticationEvent;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.sgb.kjwl.viewmodel.self.publish.TeamAuthModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.MToast;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import static com.sgb.kjwl.view.event.SuccessType.TEAM_AUTH;

import org.greenrobot.eventbus.EventBus;


public class TeamAuthActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener, View.OnClickListener {
    private Thread thread;
    private static final int MSG_LOAD_DATA = 0x0001;
    private static final int MSG_LOAD_SUCCESS = 0x0002;
    private static final int MSG_LOAD_FAILED = 0x0003;
    private ActivityTeamAuthBinding binding;
    @Inject
    TeamAuthModel model;
    // 一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 二级联动数据
    private List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 三级联动数据
    private List<List<List<AddressEntity>>> opt3 = new ArrayList<>();
    //经营类型
    private List<PickFilterEntity<List<PickFilterEntity>>> manageTypeList;
    private TeamAuthEntity teamAuthEntity = new TeamAuthEntity();
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;
    private int manageTypeIndex;
    private boolean isLoaded = false;
    private Intent intentService;
    private TeamAuthAdapter adapter;
    private List<TeamPerson> mlist=new ArrayList<>();//人员数组
    private OneAndTwoLevelMultipleChoiceWindow window;
    private List<PickFilterEntity> comReallyTypes = new ArrayList<>();
    private String id;
    private boolean isPersonAuth; //用户是否认证


    public static void start(Context context) {
        Intent intent = new Intent(context, TeamAuthActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }
    public static void start(Context context,String id) {
        Intent intent = new Intent(context, TeamAuthActivity.class);
        intent.putExtra("id",id);
        context.startActivity(intent);

    }

    public static void start(Context context, String name, String idCard, String phone) {
        Intent intent = new Intent(context, TeamAuthActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("name", name);
        intent.putExtra("idCard", idCard);
        intent.putExtra("phone", phone);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver(mUpdateReceiver, updateIntentFilter());
    }

    @Override
    protected void initView() {
        id =getIntent().getStringExtra("id");
        if (!TextUtils.isEmpty(id)){
            model.getUserInfo(id);
        }
        binding = DataBindingUtil.setContentView(TeamAuthActivity.this, R.layout.activity_team_auth);
        isPersonAuth = SharedPreferenceUtil.getInstance().getBoolean(SharedPreferenceUtil.PERSON_AUTH, false);
        model.person.set(new TeamAuthEntity.RealNameUserDTO());
        model.person.get().setIdentityCard(getIntent().getStringExtra("idCard"));
        model.person.get().setRealName(getIntent().getStringExtra("name"));
        model.person.get().setPhone(getIntent().getStringExtra("phone"));
        model.teamAuthEntity.get().setRealNameUserDTO(model.person.get());
        binding.setViewModel(model);
        binding.include.titleView.setText("团队认证");
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.manageLoc.setOnWholeItemClickListener(this);
        binding.manageType.setOnClickListener(this);
        binding.tvSureBtn.setOnClickListener(this);
        binding.tvGetCode.setOnClickListener(this);
        binding.add.setOnClickListener(this);
        binding.telephone.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.teamCount.setInputType(InputType.TYPE_CLASS_NUMBER);
        binding.idCard.setNumberAndLetter("0123456789xX");
        binding.itvItemAdminCard.setEnable(TextUtils.isEmpty(model.teamAuthEntity.get().getRealNameUserDTO().getIdentityCard()));
        binding.itvItemAdminName.setEnable(TextUtils.isEmpty(model.teamAuthEntity.get().getRealNameUserDTO().getRealName()));
        binding.itvItemAdminPhone.setEnable(TextUtils.isEmpty(model.teamAuthEntity.get().getRealNameUserDTO().getPhone()));
        //初始化人员列表
        TeamPerson teamPerson =new TeamPerson();
        teamPerson.setName("");
        teamPerson.setDomicile("");
        teamPerson.setNation("");
        teamPerson.setPhone("");
        teamPerson.setMajor("");
        teamPerson.setBirthDate(null);
        mlist.add(teamPerson);
        mlist.add(teamPerson);
        mlist.add(teamPerson);
        adapter =new TeamAuthAdapter(R.layout.item_team_auth,mlist);
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        binding.mRecyclerView.setLayoutManager(linearLayoutManager);
        binding.mRecyclerView.setAdapter(adapter);
        adapter.setOnEditePersonclicke(new TeamAuthAdapter.EditPerson() {
            @Override
            public void onDelClick(int pos) {
                  if (mlist.size()==3){
                      MToast.showToast(TeamAuthActivity.this,"最少需要三名成员");
                  }else {
                      getlist();
                      mlist.remove(pos);
                      adapter.notifyDataSetChanged();
                      setFitHeight(binding.mRecyclerView);
                  }
            }

            @Override
            public void onBorhtdayClick(int pos) {
                    showTimePop(pos);
            }


//            @Override
//            public void onnameClick(int pos) {
//                Log.e("李豆",pos+"");
//            }
//
//            @Override
//            public void onphoneClick(int pos) {
//                Log.e("李豆",pos+"");
//            }
//
//            @Override
//            public void onzhuanyeClick(int pos) {
//                Log.e("李豆",pos+"");
//            }
//
//            @Override
//            public void onminzuClick(int pos) {
//                Log.e("李豆",pos+"");
//            }
//
//            @Override
//            public void onjiguanClick(int pos) {
//                Log.e("李豆",pos+"");
//            }

        });

    }
    private void setFitHeight(RecyclerView recyclerView){
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        int itemCount = adapter.getItemCount();
        int measuredHeight = 0;
        if (itemCount >0){
            RecyclerView.ViewHolder holder = adapter.createViewHolder(recyclerView, adapter
                    .getItemViewType(0));//通过viewType类型返回ViewHolder
            adapter.onBindViewHolder(holder, 0);
            holder.itemView.measure(
                    View.MeasureSpec.makeMeasureSpec(recyclerView.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
            holder.itemView.setDrawingCacheEnabled(true);
            holder.itemView.buildDrawingCache();
            measuredHeight = holder.itemView.getMeasuredHeight();
        }

//        if (isOpen){
//            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//                    measuredHeight * 3);
//            recyclerView.setLayoutParams(layoutParams);
//            recyclerView.setNestedScrollingEnabled(true);//允许滑动
//            return isOpen = false;
//        }else{
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    measuredHeight * itemCount);
            recyclerView.setLayoutParams(layoutParams);
            recyclerView.setNestedScrollingEnabled(false);//禁止滑动
//            return isOpen = true;
//        }
    }
    public void getlist(){
        for (int i=0; i<mlist.size();i++){
            EditText name = (EditText) adapter.getViewByPosition(i,R.id.name);
            EditText phone =(EditText) adapter.getViewByPosition(i,R.id.phone);
            EditText zhuanye =(EditText) adapter.getViewByPosition(i,R.id.zhuanye);
            EditText minzu =(EditText) adapter.getViewByPosition(i,R.id.minzu);
            EditText jiguan =(EditText) adapter.getViewByPosition(i,R.id.jiguan);
            TextView shengri = (TextView) adapter.getViewByPosition(i,R.id.shengri);
            TeamPerson  teamPerson =new TeamPerson();
            teamPerson.setName(name.getText().toString());
            teamPerson.setMajor(zhuanye.getText().toString());
            teamPerson.setPhone(phone.getText().toString());
            teamPerson.setNation(minzu.getText().toString());
            teamPerson.setDomicile(jiguan.getText().toString());
            if (!TextUtils.isEmpty(shengri.getText().toString())){
                SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = format.parse(shengri.getText().toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                Long timestamp=date.getTime();
                teamPerson.setBirthDate(timestamp);
            }
            mlist.set(i,teamPerson);
        }
    }
    //时间选择器
    private void showTimePop(int pos) {
        TimePickerView timePicker = new TimePickerBuilder(TeamAuthActivity.this, (date, v) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            Date noDate = new Date();
            if (date.getTime() > noDate.getTime()) {
                MToast.showToast(TeamAuthActivity.this, "时间不能大于当前日期");
                return;
            }
            mlist.get(pos).setBirthDate(date.getTime());
            EditText name = (EditText) adapter.getViewByPosition(pos,R.id.name);
            EditText phone =(EditText) adapter.getViewByPosition(pos,R.id.phone);
            EditText zhuanye =(EditText) adapter.getViewByPosition(pos,R.id.zhuanye);
            EditText minzu =(EditText) adapter.getViewByPosition(pos,R.id.minzu);
            EditText jiguan =(EditText) adapter.getViewByPosition(pos,R.id.jiguan);
            mlist.get(pos).setName(name.getText().toString());
            mlist.get(pos).setMajor(zhuanye.getText().toString());
            mlist.get(pos).setPhone(phone.getText().toString());
            mlist.get(pos).setNation(minzu.getText().toString());
            mlist.get(pos).setDomicile(jiguan.getText().toString());
            adapter.notifyItemChanged(pos);
//            binding.itemTvAge.setContent(format);
        }).build();
        timePicker.show();
    }
    //认证成功&失败
    public void authStatus() {
        //未认证的用户 团队认证成功 需要刷新
        if (!isPersonAuth) {
            SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERSON_AUTH, true);
            AuthenticationEvent authenticationEvent = new AuthenticationEvent();
            authenticationEvent.setMessageTag(EventConstant.EVENT_USER_AUTHENTICATION);
            EventBus.getDefault().postSticky(authenticationEvent);
            EventBus.getDefault().postSticky(LoginEvent.make(EventConstant.EVENT_MAIN_INDEX, 0));  //主动通知刷新消息通讯录数据

            //给SharedPreferenceUtil中的REAL_NAME赋值 保存实名认证的状态
            SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_NAME, model.person.get().getRealName());
            //保存个人认证状态
            SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ID_CARD, model.person.get().getIdentityCard());
        }
        SuccessEntity successEntity = new SuccessEntity();
        successEntity.setTitle("认证成功");
        successEntity.setContent("您提交的认证已经成功");
        successEntity.setButtonText("确定");
        successEntity.setType(TEAM_AUTH);
        CommitSuccessActivity.start(this, successEntity);
        finish();
    }

    //注册地址
    public void showAddressPop(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> datas) {
        this.addressEntityList = datas;
        if (isLoaded) {
            showCityPop();
        } else {
            mHandler.sendEmptyMessage(MSG_LOAD_DATA);
        }
    }

    //经营业务
    public void showManageTypePop(List<PickFilterEntity<List<PickFilterEntity>>> list) {
        this.manageTypeList = list;
        showManageTypePop();
    }

    private void showCityPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(TeamAuthActivity.this, (options1, options2, options3, v) -> {
            String province;//省
            String city;//市
            String areaName;//区
            String provinceNo;//省No
            String cityNo;//市No
            String areaNameNo;//区No
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
            } else {
                province = addressEntityList.get(options1).getName();
                city = opt2.get(options1).get(options2).getName();
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                provinceNo = addressEntityList.get(options1).getNo();
                cityNo = opt2.get(options1).get(options2).getNo();
                areaNameNo = opt3.get(options1).get(options2).get(options3).getNo();
                cityPosition3 = options3;
                cityPosition2 = options2;
                cityPosition1 = options1;
                binding.manageLoc.setContent(province + city + areaName);
                binding.getViewModel().teamAuthEntity.get().setProvince(province);
                binding.getViewModel().teamAuthEntity.get().setCity(city);
                binding.getViewModel().teamAuthEntity.get().setTown(areaName);

                binding.getViewModel().teamAuthEntity.get().setProvinceCode(provinceNo);
                binding.getViewModel().teamAuthEntity.get().setCityCode(cityNo);
                binding.getViewModel().teamAuthEntity.get().setTownCode(areaNameNo);
            }
        }).build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    private void showManageTypePop() {
//        //条件选择器
//        CityPickView cityOption = new CityPickBuilder(TeamAuthActivity.this, (options1, options2, options3, v) -> {
//            String manageName;
//            if (manageTypeList.get(options1) == null) {
//                manageName = "";
//            } else {
//                manageName = manageTypeList.get(options1).getBusinessType();
//                manageTypeIndex = options1;
//                binding.manageType.setContent(manageName);
//                model.teamAuthEntity.get().setBusinessTypeNo(manageTypeList.get(options1).getNo());
//            }
//        }).build();
//        cityOption.setPicker(manageTypeList);
//        cityOption.setSelectOptions(manageTypeIndex);
//        cityOption.show();
        if (manageTypeList.isEmpty()) return;
        if (window == null) {
            window = new OneAndTwoLevelMultipleChoiceWindow(this);
        }
        window.setTitle("经营类型");
        window.setBgView(binding.grayLayout);
        window.setOnResultClick(list1 -> {
            if (list1.size() > 20){
                MToast.showToast(this, "最多选择20个");
                return;
            }
            StringBuilder builderName = new StringBuilder();
            StringBuilder builderCode= new StringBuilder();
            for (PickFilterEntity entity : list1) {
                builderName.append(entity.getName()).append(",");
                builderCode.append(entity.getNo()).append(",");
            }
            comReallyTypes.clear();
            comReallyTypes.addAll(list1);
            binding.manageType.setText(builderName.substring(0, builderName.length() - 1));
            model.teamAuthEntity.get().setBusinessTypeNo(builderCode.substring(0, builderCode.length() - 1));
            model.teamAuthEntity.get().setBusinessType(builderName.substring(0, builderName.length() - 1));
        });
        window.setHistory(manageTypeList, comReallyTypes);
        window.showPopwindow(binding.manageType, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sure_btn:
                checkData();
                break;
            case R.id.tv_get_code:
                if (TextUtils.isEmpty(binding.telephone.getContent())) {
                    MToast.showToast(TeamAuthActivity.this, "请输入手机号码");
                    return;
                } else if (!CheckUtils.checkMobile(binding.telephone.getContent())) {
                    MToast.showToast(TeamAuthActivity.this, "请输入正确的手机号码");
                    return;
                }
               /* SimpleCountDownTimer mCountDownTimerUtils = new SimpleCountDownTimer(TeamAuthActivity.this, binding.tvGetCode, 60000, 1000); //倒计时1分钟
                mCountDownTimerUtils.start();*/
                intentService = new Intent(TeamAuthActivity.this, CodeTimerService.class);
                startService(intentService);
                model.getCodeModel(binding.telephone.getContent());
                break;
            case R.id.manage_type:
                if (manageTypeList != null) {
                    showManageTypePop();
                } else {
                    model.getTeamManagementType();
                }
                break;
            case R.id.add:
                if (mlist.size()==20){
                    MToast.showToast(TeamAuthActivity.this,"最多可添加20个人员");
                    return;
                }
                getlist();
                TeamPerson teamPerson =new TeamPerson();
                teamPerson.setName("");
                teamPerson.setDomicile("");
                teamPerson.setNation("");
                teamPerson.setPhone("");
                teamPerson.setMajor("");
                teamPerson.setBirthDate(null);
                mlist.add(teamPerson);
                adapter.notifyDataSetChanged();
                setFitHeight(binding.mRecyclerView);
                break;
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.manageLoc:
                if (addressEntityList != null) {
                    showCityPop();
                } else {
                    model.getAddressModel();
                }
                break;
        }
    }

    private void checkData() {
        model.teamAuthEntity.get().setMainBusiness(binding.comServiceTypeDetail.getEditContent());
        if (TextUtils.isEmpty(binding.teamName.getContent())) {
            MToast.showToast(TeamAuthActivity.this, "请输入团队名称");
            return;
        } else if (TextUtils.isEmpty(binding.manageType.getText().toString())) {
            MToast.showToast(TeamAuthActivity.this, "请选择主营业务");
            return;
        } else if (TextUtils.isEmpty(binding.leadingPerson.getContent())) {
            MToast.showToast(TeamAuthActivity.this, "请输入负责人");
            return;
//        } else if (TextUtils.isEmpty(binding.idCard.getContent())) {
//            MToast.showToast(TeamAuthActivity.this, "请选输入身份证号码");
//            return;
//        } else if (!CheckUtils.isIDNumber(binding.idCard.getContent())) {
//            MToast.showToast(TeamAuthActivity.this, "身份认证失败，请检查姓名和身份证号是否正确");
//            return;
        } else if (TextUtils.isEmpty(binding.teamCount.getContent())) {
            MToast.showToast(TeamAuthActivity.this, "请输入团队人数");
            return;
        } else if (TextUtils.isEmpty(binding.telephone.getContent())) {
            MToast.showToast(TeamAuthActivity.this, "请输入手机号码");
            return;
        } else if (!CheckUtils.checkMobile(binding.telephone.getContent())) {
            MToast.showToast(TeamAuthActivity.this, "请输入正确的手机号码");
            return;
//        } else if (TextUtils.isEmpty(binding.etCode.getText().toString())) {
//            MToast.showToast(TeamAuthActivity.this, "请输入验证码");
//            return;
        } else if (TextUtils.isEmpty(binding.manageLoc.getContent())) {
            MToast.showToast(TeamAuthActivity.this, "请选择经营地址");
            return;
        } else if (TextUtils.isEmpty(binding.etAddress.getEditContent())) {
            MToast.showToast(TeamAuthActivity.this, "请填写详细地址");
            return;
        } else if (TextUtils.isEmpty(binding.itvItemAdminPhone.getContent())){
            MToast.showToast(TeamAuthActivity.this, "请输入管理员手机号码");
            return;
        } else if (TextUtils.isEmpty(binding.itvItemAdminName.getContent())){
            MToast.showToast(TeamAuthActivity.this, "请输入管理员姓名");
            return;
        } else if (TextUtils.isEmpty(binding.itvItemAdminCard.getContent())){
            MToast.showToast(TeamAuthActivity.this, "请输入管理员身份证号");
            return;
        }
        getlist();
        for (TeamPerson person :mlist){
            if (TextUtils.isEmpty(person.getName())){
                MToast.showToast(TeamAuthActivity.this,"请输入人员姓名");
                return;
            }
            if (TextUtils.isEmpty(person.getPhone())) {
                MToast.showToast(TeamAuthActivity.this, "请输入手机号码");
                return;
            } else if (!CheckUtils.checkMobile(person.getPhone())) {
                MToast.showToast(TeamAuthActivity.this, "请输入正确的手机号码");
                return;
            }
        }
        if (!TextUtils.isEmpty(id)){
            model.teamAuthEntity.get().setId(id);
        }
        model.teamAuthEntity.get().setPersonList(mlist);
        if (isFastDoubleClick()) {//连续点击
            return;
        }
        model.teamCertification();
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOAD_DATA:
                    if (thread == null) {//如果已创建就不再重新创建子线程了
                        thread = new Thread(() -> {
                            // 子线程中解析省市区数据
                            initJsonData();
                        });
                        thread.start();
                    }
                    break;
                case MSG_LOAD_SUCCESS:
                    showCityPop();
                    isLoaded = true;
                    thread = null;
                    break;
                case MSG_LOAD_FAILED:
                    break;
            }
        }
    };

    private void initJsonData() {
        //城市列表
        List<AddressEntity<List<AddressEntity>>> cityList;
        //区域列表
        List<List<AddressEntity>> areaList;
//        TeamAuthEntity teamAuthEntity = model.teamAuthEntity.get();
        for (int x = 0; x < addressEntityList.size(); x++) {
            cityList = new ArrayList<>();
            areaList = new ArrayList<>();
          /*  if (teamAuthEntity != null && teamAuthEntity.getProvinceName() != null && teamAuthEntity.getProvinceName().equals(addressEntityList.get(x).getName())) {
                cityPosition1 = x;
            }*/
            opt2.add(addressEntityList.get(x).getChildren());
            for (int i = 0; i < addressEntityList.get(x).getChildren().size(); i++) {
                cityList.add(addressEntityList.get(x).getChildren().get(i));
                List<AddressEntity> cityAreaList = new ArrayList<>();
                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (addressEntityList.get(x).getChildren().get(i).getChildren() == null
                        || addressEntityList.get(x).getChildren().get(i).getChildren().size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(addressEntityList.get(x).getChildren().get(i).getChildren());
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
 /*       if (addDeviceEntity != null) {
            if (addDeviceEntity.getCityName() != null)
                for (int x2 = 0; x2 < opt2.size(); x2++) {
                    if (addDeviceEntity.getCityName().equals(addressEntityList.get(cityPosition1).getChildren().get(x2).getName())) {
                        cityPosition2 = x2;
                        break;
                    }
                }
            if (addDeviceEntity.getDistrictName() != null)
                for (int x3 = 0; x3 < opt3.size(); x3++) {
                    if (addDeviceEntity.getDistrictName().equals(addressEntityList.get(cityPosition1).getChildren().get(cityPosition2).getChildren().get(x3).getName())) {
                        cityPosition3 = x3;
                        break;
                    }
                }
        }*/
        mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
    }

    private static long lastClickTime = 0;

    private static boolean isFastDoubleClick() {
        long time = System.currentTimeMillis();
        long timeD = time - lastClickTime;
        if (0 < timeD && timeD < 1000) {
            return true;
        }
        lastClickTime = time;
        return false;
    }
  public void showDetail(TeamAuthEntity entity) {
           model.teamAuthEntity.get().setName(entity.getTeamName());
           model.teamAuthEntity.get().setBusinessType(entity.getTeamBusinessType());
           model.teamAuthEntity.get().setScale(entity.getTeamScale());
           model.teamAuthEntity.get().setContact(entity.getTeamContact());
           binding.teamName.getContent_edt().setText(entity.getTeamName());
           binding.manageType.setText(entity.getTeamBusinessType());
           binding.comServiceTypeDetail.getEditText().setText(entity.getMainBusiness());
           binding.leadingPerson.getContent_edt().setText(entity.getTeamContact());
           binding.teamCount.getContent_edt().setText(entity.getTeamScale());
           binding.telephone.getContent_edt().setText(entity.getPhone());
           binding.manageLoc.getContent_edt().setText(entity.getBusinessAddress());
           binding.etAddress.getEditText().setText(entity.getBusinessAddressSup());
        if (entity.getRealNameUserDTO() != null){
            binding.itvItemAdminPhone.setContent(entity.getRealNameUserDTO().getPhone());
            binding.itvItemAdminName.setContent(entity.getRealNameUserDTO().getRealName());
            binding.itvItemAdminCard.setContent(entity.getRealNameUserDTO().getIdentityCard());
        }
        mlist.clear();
        mlist.addAll(entity.getPersonList());
        adapter.notifyDataSetChanged();
        setFitHeight(binding.mRecyclerView);
    }

    // 注册广播
    private static IntentFilter updateIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CodeTimerService.IN_RUNNING);
        intentFilter.addAction(CodeTimerService.END_RUNNING);
        return intentFilter;
    }

    // 广播接收者
    private final BroadcastReceiver mUpdateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            switch (action) {
                case CodeTimerService.IN_RUNNING:
                    if (binding.tvGetCode.isEnabled())
                        binding.tvGetCode.setEnabled(false);
                    // 正在倒计时
                    binding.tvGetCode.setTextColor(Color.GRAY);
                    binding.tvGetCode.setText(intent.getStringExtra("time") + "秒后重新发送");
                    Log.e("团队认证验证码倒计时", "倒计时中(" + intent.getStringExtra("time") + ")");
                    break;
                case CodeTimerService.END_RUNNING:
                    // 完成倒计时
                    binding.tvGetCode.setTextColor(getResources().getColor(R.color.new_blue));
                    binding.tvGetCode.setEnabled(true);
                    binding.tvGetCode.setText("获取验证码");
                    break;
            }
        }
    };

/*    @Override
    protected void onResume() {
        super.onResume();
        unregisterReceiver(mUpdateReceiver);
    }*/

  /*  @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mUpdateReceiver);
    }*/

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUpdateReceiver);
    }
}
